﻿using UnityEngine;
using System.Collections;
using LuaInterface;

namespace AQ
{
	
	public class BundleManager : MonoBehaviour
	{
		static BundleManager instance;

		void Awake()
		{
			instance = this;
		}

		IEnumerator Start()
		{
			AssetBundleManager.SetDevelopmentAssetBundleServer ();
			AssetBundleLoadManifestOperation request = AssetBundleManager.Initialize();
			if (request == null) {
				yield break;
			}
			yield return StartCoroutine (request);
		}

		//IEnumerator LoadAssetAsyncCoroutine(string assetBundleName, string assetName, System.Type type, System.Action<Object> action)
		IEnumerator LoadAssetAsyncCoroutine(string assetBundleName, string assetName, System.Type type, LuaFunction func)
		{
			float startTime = Time.realtimeSinceStartup;

			AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync (assetBundleName, assetName, type);
			if (request == null) {
				func.Call (null, true);
				yield break;
			}
			yield return StartCoroutine (request);

			var asset = request.GetAsset<Object> ();
			float elapsedTime = Time.realtimeSinceStartup - startTime;
			Debug.LogFormat ("[AssetBundleManager] {0} {1} loaded successfully in {2} seconds", assetName, (asset == null ? " was not" : " was"), elapsedTime);

			func.Call (asset, null);

		}

		//IEnumerator LoadLevelAsyncCoroutine(string assetBundleName, string levelName, bool isAdditive, System.Action action)
		IEnumerator LoadLevelAsyncCoroutine(string assetBundleName, string levelName, bool isAdditive, LuaFunction func)
		{
			float startTime = Time.realtimeSinceStartup;

			AssetBundleLoadOperation request = AssetBundleManager.LoadLevelAsync (assetBundleName, levelName, isAdditive);
			if (request == null) {
				func.Call ("fail");
				yield break;
			}
			yield return StartCoroutine (request);

			float elapsedTime = Time.realtimeSinceStartup - startTime;
			Debug.LogFormat ("[AssetBundleManager] {0} loaded successfully in {1} seconds", levelName, elapsedTime);

			func.Call ("success");
		}

		IEnumerator LoadAssetBundleAsyncCoroutine(string assetBundleName, LuaFunction func)
		{
			float startTime = Time.realtimeSinceStartup;

			AssetBundleLoadedOperation request = AssetBundleManager.LoadAssetBundleAsync (assetBundleName);
			if (request == null) {
				func.Call (null, true);
				yield break;
			}
			yield return StartCoroutine (request);

			var asset = request.assetBundle.m_AssetBundle;
			float elapsedTime = Time.realtimeSinceStartup - startTime;
			Debug.LogFormat ("[AssetBundleManager] AssetBundle {0} {1} loaded successfully in {2} seconds", assetBundleName, (asset == null ? " was not" : " was"), elapsedTime);

			func.Call (asset, null);
		}

		static public void LoadAssetAsync(string assetBundleName, string assetName, System.Type type, LuaFunction func)
		{
			instance.StartCoroutine (instance.LoadAssetAsyncCoroutine(assetBundleName, assetName, type, func));
		}

		static public void LoadLevelAsync(string assetBundleName, string levelName, bool isAdditive, LuaFunction func)
		{
			instance.StartCoroutine (instance.LoadLevelAsyncCoroutine(assetBundleName, levelName, isAdditive, func));
		}

		static public void LoadAssetBundleAsync(string assetBundleName, LuaFunction func)
		{
			instance.StartCoroutine (instance.LoadAssetBundleAsyncCoroutine(assetBundleName, func));
		}
	}

}